# This PS installs the ARM GNU GCC toolchain if it't not already available

# check if path already exists
$GnuGccPathExists = Test-Path $env:GNU_GCC_TOOLCHAIN_PATH -ErrorAction SilentlyContinue

If($GnuGccPathExists -eq $False)
{
    Write-Host "Downloading ARM GNU GCC toolchain..."

    $url = "https://developer.arm.com/-/media/Files/downloads/gnu-rm/6-2017q2/gcc-arm-none-eabi-6-2017-q2-update-win32.zip?revision=d8809bf7-a431-49ee-98d5-0475d839f8f1?product=GNU Arm Embedded Toolchain,ZIP,,Windows,6-2017-q2-update"
    $output = "$PSScriptRoot\gcc-arm.zip"
    # download zip with toolchain
    (New-Object Net.WebClient).DownloadFile($url, $output)

    Write-Host "Installing ARM GNU GCC toolchain..."
    
    # unzip toolchain
    Expand-Archive $output -DestinationPath $env:GNU_GCC_TOOLCHAIN_PATH

    # delete the samples and docs folder
    $toDelete = $env:GNU_GCC_TOOLCHAIN_PATH + '\share'
    Remove-Item -Recurse -Force $toDelete
}
